/**
 *  (c)  
 * <p>
 * 
 * <p>
 * 
 */

package com.nn.smart.bus.commons.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.nn.smart.bus.commons.mybatis.entity.BaseEntity;
import com.nn.smart.bus.commons.mybatis.entity.BaseEpdcEntity;
import com.nn.smart.bus.commons.mybatis.enums.DelFlagEnum;
import com.nn.smart.bus.commons.mybatis.enums.EpdcDelFlagEnum;
import com.nn.smart.bus.commons.tools.constant.Constant;
import com.nn.smart.bus.commons.tools.constant.FieldConstant;
import com.nn.smart.bus.commons.tools.constant.NumConstant;
import com.nn.smart.bus.commons.tools.security.user.SecurityUser;
import com.nn.smart.bus.commons.tools.security.user.UserDetail;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 公共字段，自动填充值
 *
 * @author nanning
 * @since 1.0.0
 */
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {
    private final static String CREATE_DATE = "createDate";
    private final static String CREATOR = "creator";
    private final static String UPDATE_DATE = "updateDate";
    private final static String UPDATER = "updater";
    private final static String DEPT_ID = "deptId";

    @Override
    public void insertFill(MetaObject metaObject) {
        UserDetail user = SecurityUser.getUser();
        Date date = new Date();
        if (metaObject.getOriginalObject() instanceof BaseEpdcEntity) {
            String userId = user == null ? Constant.APP_USER_FLAG : String.valueOf(user.getId());
            Long deptId = user == null ? null : user.getDeptId();
            setFieldValByName(FieldConstant.CREATED_TIME_HUMP, date, metaObject);
            setFieldValByName(FieldConstant.CREATED_BY_HUMP, userId, metaObject);
            setFieldValByName(FieldConstant.UPDATED_TIME_HUMP, date, metaObject);
            setFieldValByName(FieldConstant.UPDATED_BY_HUMP, userId, metaObject);
            setFieldValByName(FieldConstant.REVISION_HUMP, NumConstant.ZERO, metaObject);
            //删除标识
            setFieldValByName(FieldConstant.DEL_FLAG_HUMP, EpdcDelFlagEnum.NORMAL.value(), metaObject);
            // 数据权限标志
            setInsertFieldValByName(DEPT_ID, deptId, metaObject);
        } else {
            if (user == null) {
                return;
            }
            //创建者
            setFieldValByName(CREATOR, user.getId(), metaObject);
            //创建时间
            setFieldValByName(CREATE_DATE, date, metaObject);
            //创建者所属部门
            setInsertFieldValByName(DEPT_ID, user.getDeptId(), metaObject);
            //更新者
            setFieldValByName(UPDATER, user.getId(), metaObject);
            //更新时间
            setFieldValByName(UPDATE_DATE, date, metaObject);
            //删除标识
            setFieldValByName(FieldConstant.DEL_FLAG_HUMP, DelFlagEnum.NORMAL.value(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        Object originalObject = metaObject.getOriginalObject();
        Long id = SecurityUser.getUserId();

        boolean fillEntity = false;
        boolean fillEsuaEntity = false;
        if (originalObject instanceof BaseEntity) {
            fillEntity = true;
        } else if (originalObject instanceof BaseEpdcEntity) {
            fillEsuaEntity = true;
        } else {
            if (metaObject.hasGetter(Constants.ENTITY)) {
                Object et = metaObject.getValue(Constants.ENTITY);
                if (et instanceof BaseEpdcEntity) {
                    fillEsuaEntity = true;
                } else if (et instanceof BaseEntity) {
                    fillEntity = true;
                }
            }
        }
        if (fillEntity) {
            id = null == id ? NumConstant.ZERO_L : id;
            //更新者
            setFieldValByName(UPDATER, id, metaObject);
            //更新时间
            setFieldValByName(UPDATE_DATE, new Date(), metaObject);
        } else if (fillEsuaEntity) {
            String userId = null == id ? Constant.APP_USER_FLAG : String.valueOf(id);
            setFieldValByName(FieldConstant.UPDATED_BY_HUMP, userId, metaObject);
            setFieldValByName(FieldConstant.UPDATED_TIME_HUMP, new Date(), metaObject);
        }
    }
}
